#!/bin/sed -f

# Select the lines which ends in DEBUG
# and Delete the marker
:y
s/\/\/#D//
#if we dont fing a marcar proceed with the next line
T

#this runs only when we find a marker
:x
#erase the possible {
s/{/{/
#if we erased then jump to z
t z
# else add a new line to the pattern space (N) and jump to x
# erase the comments
s/\/\/.*$//
N
bx

#this is called later
#:a
#   #s/^/m4_dProto_const(/
#   b b

:z

   #replace tabs by spaces
   y/\t/ /
   #eliminate trailings
   s/^ *//
   s/ *$//
   # Eliminating spaces before the , or )
   s/[ ]*\([,)]\)[ ]*/\1/g
   # Elminating spaces after newlines 
   s/\n */\n/g
   # Elminating double spaces
   s/  +//g

   #adding a `
   s/^/`/

   # preparing the const case
   s/)const {/)!/
   s/\(^[^!]*\)!/_const \1/

   # Separting the argument name with a ,
   s/\( [_a-zA-Z0-9]*\)\([,)]\)/,\1\2/g
   #repacing , for ,\n
   #s/,/,\n/g
   #quoting arguments between ( , or )
   s/,/',`/g
   #s/,\n/',\n`/g
   s/` *\n/`/g
   # replacing the first ( by ',`
   s/(/',`/
   # replacing the last ) by ')
   s/)/')/
   # Eliminating spaces before the '
   s/[ ]*\(,\)/\1/g
   # Eliminating spaces after the `
   s/`[ ]*/`/g
   # m4_dProto
   s/^/m4_dProto/
   # add the space
   s/\(^[^ `]*\)/\1(/
   # remove any { which will be added by the macro
   s/{//g
   # adding dnl at the end
   s/$/dnl/

   # Defining the inherit macro
   s/\(.*) *:\)\(.*) *\)\(dnl\)/define(`m4_inherited',`: m4_my_join(`\2')') \n\1/
   # erasing posible ending :'s
   s/: *$/\ndefine(`m4_inherited',`')/

   # adding dnl at the end
   s/\n/dnl\n/g
   s/\(.*$\)/\1dnl/
   s/dnldnl/dnl/g

# this sed script transforms lines like:
#     int function ( int arg  , const char arg2 ) { //#D
# to
#     m4_dProto(`int function ',`int',`arg',`const char',`arg2')
